﻿using System;
using System.Collections.Generic;
using System.Linq;
using HiStaff.Base;
using HiStaff.Control;
using HiStaff.Domain.Sys;
using HiStaff.Util;
using HiStaff.iSystem;
using Janus.Windows.GridEX;
using System.Data;

namespace HiStaff.iSecure
{
    public partial class PermissionFunction : FormBase
    {
        public PermissionFunction()
        {
            InitializeComponent();
        }
        protected override bool FormLoad()
        {
            base.CreateCommandBar(CommandBarManager.ActionCommand.Edit,
                CommandBarManager.ActionCommand.Separator,
                CommandBarManager.ActionCommand.Save,
                CommandBarManager.ActionCommand.Cancel,
                CommandBarManager.ActionCommand.Separator,
                CommandBarManager.ActionCommand.Exit);

            SetToolStripButtonBar(CommandBarManager.ActionCommand.Cancel);
            InitGrid();
            BindData();
            return base.FormLoad();
        }
        private void InitGrid()
        {
            var lstUser = Service.SystemClient.GetUser(new SY_USER { STATUS = 1 });
            List<SY_MODULE> lstMdl = Service.SystemClient.GetModule(new SY_MODULE());
            grdData.RootTable.Columns["MODULEID"].HasValueList = true;
            grdData.RootTable.Columns["MODULEID"].ValueList.PopulateValueList(lstMdl, "ID", "NAME");
            GridEXColumn col;
            foreach (var item in lstUser)
            {
                col = new GridEXColumn();
                col.Key = item.USERNAME;
                col.DataMember = item.USERNAME;
                col.Caption = item.USERNAME;
                col.HeaderStyle.TextAlignment = TextAlignment.Center;
                col.ColumnType = ColumnType.CheckBox;
                col.EditType = EditType.CheckBox;
                col.FilterEditType = FilterEditType.SameAsEditType;
                col.CheckBoxFalseValue = 0m;
                col.CheckBoxTrueValue = 1m;
                col.CheckBoxTriState = false;
                col.Width = 80;
                col.HeaderToolTip = item.FULLNAME + " - " + item.EMAIL;
                col.CellToolTipText = item.FULLNAME + " - " + item.EMAIL;
                grdData.RootTable.Columns.Add(col);
            }
        }
        private void BindData()
        {
            grdData.DataSource = Service.ExcuteStoreToDataTable("PRS_SY_USER_FUNC");
            grdData.Refresh();
        }
        protected override void SetToolStripButtonBar(CommandBarManager.ActionCommand cmd)
        {
            switch (cmd)
            {
                case CommandBarManager.ActionCommand.Edit:
                    SetEnabledCommandSave = true;
                    SetEnabledCommandCancel = true;
                    SetEnabledCommandEdit = false;
                    break;
                case CommandBarManager.ActionCommand.Cancel:
                case CommandBarManager.ActionCommand.Save:
                    SetEnabledCommandSave = false;
                    SetEnabledCommandCancel = false;
                    SetEnabledCommandEdit = true;
                    break;
            }
        }
        protected override bool EditButtonClick()
        {
            grdData.AllowEdit = InheritableBoolean.True;
            return true;
        }
        protected override bool CancelButtonClick()
        {
            grdData.AllowEdit = InheritableBoolean.False;
            BindData();
            return true;
        }
        protected override bool SaveButtonClick()
        {
            ExcuteAsynchronous();
            grdData.AllowEdit = InheritableBoolean.False;
            return true;
        }
        protected override bool AsynchronousFunctionWithBGW(System.ComponentModel.BackgroundWorker bgWorker)
        {
            DataTable dt = (DataTable)grdData.DataSource;
            int i = dt.Rows.Count + 1;
            int j = 0;
            int percent;
            decimal? funcId;
            foreach (DataRow drow in dt.Rows)
            {
                j++;
                funcId = ConvertHelper.ToDecimal(drow["ID"]);
                if (funcId.HasValue)
                {
                    Service.ExcuteStoreNonQuery("APRD_SY_USER_FUNC", new SY_USER_FUNC
                    {
                        FUNCID = funcId
                    });
                    foreach (DataColumn col in drow.Table.Columns)
                    {
                        if (drow[col.ColumnName].Equals(1m))
                        {
                            Service.ExcuteStoreNonQuery("APRU_SY_USER_FUNC", new SY_USER_FUNC
                            {
                                FUNCID = funcId,
                                USERNAME = col.ColumnName
                            });
                        }
                    }
                }
                percent = (j * 100 / i);
                bgWorker.ReportProgress(percent, "Đang thực hiện");
            }
            bgWorker.ReportProgress(100, "Hoàn tất");
            return true;
        }
    }
}
